# encoding=utf-8
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
import unittest, time
from Configure import Attribute as att
from Configure import UserInfo
from BaseAndUnit import BaseParent
from GeneralFunction.Operation import operation

cannotLoginCount = 0


class BaseLogin(BaseParent.Base):
    def login(self):
        driver = self.driver
        global cannotLoginCount
        if cannotLoginCount > 2:
            raise ValueError("尝试登录三次后仍未成功")
            return False
        elif driver.current_activity == att.activity_welcome:
            if operation.isElementExsit(self,
                                        att.login_needmodify_id):
                operation.find_element(self, att.login_needmodify_donot_id).click()
                return self.login()
            else:
                return self.login()
        elif driver.current_activity == att.activity_login:
            if operation.isElementExsit(self,
                                        att.login_needmodify_id):
                operation.find_element(self, att.login_needmodify_donot_id).click()
                return self.login()
            else:
                self.login_login(driver)
                return self.login()
        elif driver.current_activity == att.activity_chooseStu:
            self.driver.find_elements_by_id(att.choosestudent_list_item)[0].click()
            return self.login()
        elif driver.current_activity == att.activity_main:
            if operation.isElementExsit(self, att.login_autologin_sucess_id):
                return True
            else:
                return False
                raise ValueError("登录成功但是出现异常")
        else:
            return False
            raise ValueError("程序出现异常，登录时不知道到哪里了")

    def login_login(self, dr):
        dr.find_element_by_id(att.login_username_id).click()
        dr.find_element_by_id(att.login_username_id).clear()
        dr.find_element_by_id(att.login_username_id).send_keys(UserInfo.LoginUserName)
        dr.find_element_by_id(att.login_pass_id).click()
        dr.find_element_by_id(att.login_pass_id).clear()
        dr.find_element_by_id(att.login_pass_id).send_keys(UserInfo.LoginPass)
        dr.find_element_by_id(att.login_loginbutton_id).click()
        global cannotLoginCount
        cannotLoginCount += 1
        time.sleep(1)


if __name__ == '__main__':
    unittest.main()
